package ec.edu.uce.tour.web.menu.componente;

import java.util.List;

import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.el.MethodNotFoundException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.FacesEvent;

import org.primefaces.event.MenuActionEvent;
import org.primefaces.model.menu.MenuItem;
import org.primefaces.model.menu.MenuModel;

public abstract class AbstractMenu extends UIComponentBase{

public static final String MENU_ACTIVO = "menu_tour_active";
public static final String MENU_CUBIERTA = "menu_tour_wrap";
public static final String MENU_LINK = "menu_tour_link";
public static final String MENU_BOX = "menu_tour_box";
public static final String MENU_CLASS = "menu_tour";
public static final String MENU_DESC = "menu_tour_desc";
	
	public List getElements() {
        MenuModel model = getModel();
        if(model != null)
            return model.getElements();
        else
            return getChildren();
    }
    
    public int getElementsCount() {
        List elements = getElements();
        
        return (elements == null) ? 0 : elements.size();
    }
    
	public abstract MenuModel getModel();
	
	public boolean isDynamic() {
		return this.getValueExpression("model") != null;
	}

    @Override
    public void broadcast(FacesEvent event) throws AbortProcessingException {
        if(event instanceof MenuActionEvent) {
            FacesContext facesContext = getFacesContext();
            ELContext eLContext = facesContext.getELContext();
            MenuActionEvent menuActionEvent = (MenuActionEvent) event;
            MenuItem menuItem = menuActionEvent.getMenuItem();
            String actionExpressionString = menuItem.getCommand();
            MethodExpression noArgExpr = facesContext.getApplication().getExpressionFactory().
                            createMethodExpression(eLContext,actionExpressionString, 
                                                        String.class, new Class[0]);
            Object outcome = null;
            
            try {
                outcome = noArgExpr.invoke(eLContext, null);
            } 
            catch(MethodNotFoundException methodNotFoundException) {
                MethodExpression argExpr = facesContext.getApplication().getExpressionFactory().
                            createMethodExpression(eLContext, actionExpressionString, 
                                                        String.class, new Class[]{ActionEvent.class});
                
                outcome = argExpr.invoke(eLContext, new Object[]{event});
            }
            finally {
                if(outcome != null) {
                    facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, actionExpressionString, outcome.toString());
                }
            }
            
        }
        else {
            super.broadcast(event);
        }
    }
}

